/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rm.freedrawsample.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.util.ArrayList;
import java.util.List;

/**
 * ValidatingObjectInputStream.
 *
 * @author a
 * @version v
 * @since 22/11/16
 */
public class ValidatingObjectInputStream extends ObjectInputStream {
    /**
     * acceptMatchers。
     */
    private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
    /**
     * acceptMatchers。
     */
    private final List<ClassNameMatcher> rejectMatchers = new ArrayList<>();

    /**
     * ValidatingObjectInputStream.
     *
     * @param input input
     * @throws IOException IOException
     */
    public ValidatingObjectInputStream(final InputStream input) throws IOException {
        super(input);
    }

    private void validateClassName(final String name) throws InvalidClassException {
        for (final ClassNameMatcher matcher : rejectMatchers) {
            if (matcher.matches(name)) {
                invalidClassNameFound(name);
            }
        }
        boolean ok = false;
        for (final ClassNameMatcher matcher : acceptMatchers) {
            if (matcher.matches(name)) {
                ok = true;
                break;
            }
        }
        if (!ok) {
            invalidClassNameFound(name);
        }
    }

    /**
     * invalidClassNameFound.
     *
     * @param className className
     * @throws InvalidClassException InvalidClassException
     */
    protected void invalidClassNameFound(final String className) throws InvalidClassException {
        throw new InvalidClassException("Class name not accepted: " + className);
    }

    @Override
    protected Class<?> resolveClass(final ObjectStreamClass osc) throws IOException, ClassNotFoundException {
        validateClassName(osc.getName());
        return super.resolveClass(osc);
    }

    /**
     * accept.
     *
     * @param classes classes
     * @return ValidatingObjectInputStream
     */
    public ValidatingObjectInputStream accept(final Class<?>... classes) {
        for (final Class<?> clazz : classes) {
            acceptMatchers.add(new FullClassNameMatcher(clazz.getName()));
        }
        return this;
    }

    /**
     * reject.
     *
     * @param classes classes
     * @return ValidatingObjectInputStream
     */
    public ValidatingObjectInputStream reject(final Class<?>... classes) {
        for (final Class<?> clazz : classes) {
            rejectMatchers.add(new FullClassNameMatcher(clazz.getName()));
        }
        return this;
    }

}